import Observer from '../common/Observer';
import pxmu from 'pxmu';
import { data } from 'jquery';
// console.log(data);
window.addEventListener("load", function () {
    // 创建相册功能
    let albumInput = document.getElementById("albumInput");
    let albumCreateBtn = document.getElementById("albumCreateBtn");
    let select = this.document.getElementById('select')


    albumCreateBtn.onclick = function () {
        fetch('/guyun/create_album?authrization=' + localStorage.getItem("token") + "&name=" + albumInput.value)
            .then(res => res.json() )
            .then(({ data, error }) => {
                console.log(data);
                if (!error) {
                    pxmu.success(data)
                    get_albums()
                    return;
                }
                pxmu.fail('创建失败');
                // get_albums()
            })
            .catch((error) => {
                console.log(error)
            })
    }

    function get_albums(){
        fetch('/guyun/get_albums?authrization=' + localStorage.getItem("token"))
        .then(res => res.json())
        .then(({ error, data }) => {
            if (!error) {
                select.innerHTML = data.map((val, index) => `<option>${data[index]['albumname']}</option>`).join("")
            }
        })
        .catch((error) => console.log(error))
    }
    get_albums()
    // var albumId;
    // 获得当前用户的所有相册
   
    // fetch('/guyun/get_albums?authrization=' + localStorage.getItem("token"))
    //     .then(res => res.json())
    //     .then(({ error, data }) => {

    //         if (!error) {
    //             // console.log(Array.isArray(data));
    //             function getAlbumId(){
    //                 albumId=data.map((val,index)=>{
    //                     return (val["_id"],index);
    //                 })
    //                 console.log(albumId);
    //             }
    //             // getAlbumId()
                
                 
    //             select.innerHTML += data.map((val, index) => `<option>${data[index]['albumname']}</option>`).join("")
    //         }
    //     })
    //     .catch((error) => console.log(error))

        


    
    // 上传图片到指定文件夹
    let file = document.getElementById('file')
    let uploadBtn = document.getElementById('uploadBtn')

    uploadBtn.onclick = function () {
        // 用表单来初始化
        var formData = new FormData();
        // 把token加入进去
        formData.append("authrization", localStorage.getItem("token"))
        // 把相册名加入FormData实例
        formData.append("albumname", select.value);

        // 把文件加入FormData实例
        [...file.files].forEach(function (val) {
            formData.append("file", val)
        })
 
        fetch('/guyun/upload_img', {
            method: 'post',
            body: formData,
        })
        .then(res =>res.json())
        .then(({ error, data }) => {
            if (!error) {
                console.log('上传成功');
                pxmu.success(data)
            }
        })
        .catch((error) => console.log(error))

        
    }

    // getAlbumId()
    // console.log(getAlbumId());
    
    
    // let deleteBtn=this.document.getElementById('deleteBtn')
    // select.onchange=function(){
    //     console.log(albumname.indexOf(data));
    // }
    // deleteBtn.onclick=function(){
    //     getAlbumId()
        
    //     fetch('/guyun/del_album?authrization=' + localStorage.getItem("token") + "&name=" +select.value+"id="+data.data)
    //     .then()
    // }







 





    /* 

        // 删除相册
        // let select=this.document.getElementById('select')
    let deleteBtn=document.getElementById('deleteBtn')
    deleteBtn.onclick=function(){
        QF.get("/get_albums", {name: albumInput.value, authrization: localStorage.getItem("token")}, function(data) {
            console.log('get到了');
            select.innerHTML = data.data.map(val => `<option>${val}</option>`).join("")
        
            console.log(select);
            QF.get("/delete_album", {name: albumInput.value, authrization: localStorage.getItem("token")},function(data){
                console.log(albumInput.value);
                console.log('进入delete');
                // if(data.error===0){
                    console.log(data.data);
                   
                    select.onchange=function(){
                        console.log('删除成功le');
    
                        let albumArr=data.data;
                        delete albumArr.indexOf(select.value)
                        console.log('删除成功');
                        // console.log(data.data.indexOf(select.value));
                       
                    // }
                    
                }
            })
        })
       
        
    }
    
    
    
    
    
    // 展示我的作品
    let myWork=this.document.getElementById('myWork');
    console.log(myWork);
    myWork.onclick=function(){
        QF.get("/get_albums", {name: albumInput.value, authrization: localStorage.getItem("token")}, function(data,err) {
            if(!err){
                console.log(data.data);
                QF.get('/show_album',{name: albumInput.value,authrization: localStorage.getItem("token")}, function(data){
                    console.log(data);
                })
            }
            
        })
    
        
    } */



})

